package com.ciwong.epaper.modules.me.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by keepon on 2018/8/7.
 */
public abstract class BaseNewBookItemInAdapter<T> extends RecyclerView.Adapter<BaseNewBookItemInAdapter.ViewHolder> {
    protected Context mContext;
    private List<T> mDatas;

    public BaseNewBookItemInAdapter(Context context, List<T> datas) {
        mContext = context;
        mDatas = datas;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder newBookDeskViewHolder = creatViewHolde(parent, viewType);
        return newBookDeskViewHolder;
    }

    protected abstract ViewHolder creatViewHolde(ViewGroup parent, int viewType);

    @Override
    public void onBindViewHolder(BaseNewBookItemInAdapter.ViewHolder holder, int position) {
        holder.bindData_(position, mDatas.get(position));
    }

    @Override
    public int getItemCount() {
        return mDatas != null ? mDatas.size() : 0;
    }

    public void updateList(List<T> bookmarkBeanList) {
        mDatas.clear();
        mDatas.addAll(bookmarkBeanList);
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {

            super(itemView);
        }

        public void bindData_(final int position, final T bookmarkBean) {

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(position, bookmarkBean);
                    }
                }

            });
            bindData(itemView, position, bookmarkBean);
        }

    }

    public abstract void bindData(View itemView, int position, T bookmarkBean);

    public void setOnItemClickListener(OnItemClickListener<T> onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener<T> {
        void onItemClick(int positon, T bookmarkBean);
    }
}
